package com.ys.service.wallet.eth;

import cn.hutool.json.JSONUtil;
import com.ys.entity.block.eth.EthTransactionReceipt;
import com.ys.enu.CurrencyEnum;
import com.ys.exception.base.BizException;
import com.ys.service.WalletAddressServiceImap;
import com.ys.service.WalletWithdrawServiceImap;
import com.ys.service.wallet.base.AbstractWalletService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
@Slf4j
@Data
@ConfigurationProperties(prefix="wallet.eth")
public class EthWalletServiceBase extends AbstractWalletService {

    @Autowired
    private WalletAddressServiceImap walletAddressServiceImap;
    @Autowired
    private WalletWithdrawServiceImap walletWithdrawServiceImap;

    private Integer currency;
    private CurrencyEnum currencyEnum;
    private String server;
    private String withDrawAddressPwd;
    private String withDrawAddress;
    private String depositAddress;
    
    //eth 数字都是16进制
    public static final int PASS_RADIX = 16;
    public static final double FEE = 0.000000000000021000;
    //eth 单位wei 转成 币
    static final double PASS_VALUE = 1.0E18;

    static final double maxPrice = 0.00000020d;
    static final double defPrice = 0.00000010d;
    static final double minPrice = 0.00000005d;

    @Override
    public String auth() {
        return null;
    }

    @Override
    public String server() {
        return server;
    }

    public String withDrawAddressPwd(){
        return withDrawAddressPwd;
    }

    public Double maxPrice(){
        return maxPrice;
    }
    public double fee(){
        return FEE*2*10;
    }

    public void setCurrency(Integer currency) {
        this.currency = currency;
        this.currencyEnum=CurrencyEnum.parseCurrency(currency);
    }

    @Override
    public CurrencyEnum currencyEnum() {
        return this.getCurrencyEnum();
    }


    /**
     * 获取交易明细 Receipt 判断是否是智能合约交易
     * @param txHash
     * @return
     */
    public EthTransactionReceipt ethGetTransactionReceipt(String txHash) throws Exception {
        try {
            Object param = new Object[] {txHash};
            log.info(JSONUtil.toJsonStr(createRPCNOAuth().invoke("eth_getTransactionReceipt", param, HashMap.class)));
            return  createRPCNOAuth().invoke("eth_getTransactionReceipt", param, EthTransactionReceipt.class);
        } catch ( Throwable e) {
            log.info("获取交易明细Receipt失败 txhash="+txHash,e);
            throw new Exception("获取交易明细Receipt失败 txhash="+txHash);
        }
    }


}
